<template>
  <div>
    <!-- 搜索栏   -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="82px">

      <el-form-item label="收礼人" prop="receiveName">
        <el-input
          v-model="queryParams.receiveName"
          size="mini"
          clearable
          placeholder="请输入名称"
          label-width="100px"
          @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item>
      <el-form-item label="收礼类型" prop="billType">
        <el-select
          v-model="queryParams.billType"
          placeholder="请选择收礼类型"
          size="mini"
          @change="handleQuery"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="结婚" value=1></el-option>
          <el-option label="生子" value=2></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否出席" prop="isAttend">
        <el-select
          v-model="queryParams.isAttend"
          placeholder="请选择出席类型"
          size="mini"
          @change="handleQuery"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="出席" value=1></el-option>
          <el-option label="未出席" value=0></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 搜索栏   -->

    <!-- 新增 导出按钮操作   -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 新增 导出按钮操作   -->

    <!-- 列表数据   -->
    <el-table v-loading="loading" :data="billRecordList" @selection-change="handleSelectionChange">
      <el-table-column label="收礼人" width="110" align="center" prop="receiveName"/>
      <el-table-column label="收礼类型" width="110" align="center" prop="billType">
        <template slot-scope="scope">
          <span v-if="scope.row.billType == 1">结婚</span>
          <span v-if="scope.row.billType == 2">生子</span>
        </template>
      </el-table-column>
      <el-table-column label="礼金（元）" width="160" align="center" prop="money"/>
      <el-table-column label="是否出席" width="110" align="center" prop="isAttend">
        <template slot-scope="scope">
          <span v-if="scope.row.isAttend == 0">未出席</span>
          <span v-if="scope.row.isAttend == 1">出席</span>
        </template>
      </el-table-column>
      <el-table-column label="送礼时间" align="center" prop="businessTime"/>
      <el-table-column label="备注信息" align="center" prop="memo"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 列表数据   -->

    <!-- 新增/编辑弹窗   -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="收礼人" prop="receiveName">
              <el-input v-model="form.receiveName" placeholder="请输入收礼人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收礼类型" prop="billType">
              <el-select v-model="form.billType" placeholder="请选择">
                <el-option label="结婚" :value=1></el-option>
                <el-option label="生子" :value=2></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="礼金" prop="money">
              <!--              <el-input v-model="form.money" placeholder="请输入礼金" />-->
              <el-input placeholder="请输入礼金" v-model="form.money" type="number" step="10">
                <template slot="append">元</template>
              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否出席" prop="isAttend">
              <el-select v-model="form.isAttend" placeholder="请选择">
                <el-option label="未出席" :value=0></el-option>
                <el-option label="出席" :value=1></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送礼时间" prop="businessTime">
              <el-date-picker clearable size="small"
                              v-model="form.businessTime"
                              type="datetime"
                              default-time="00:00:00"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择送礼时间" style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="memo">
              <el-input v-model="form.memo" placeholder="请输入备注信息"/>
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"  @click="submitForm">确 定</el-button>
<!--  v-preventClick      -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增/编辑弹窗   -->


    <!-- 分页   -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.perPage"
      @pagination="getList"
    />
    <!-- 分页   -->
  </div>
</template>

<script>
import {deleteBillRecord, listBillRecord} from '@/api/bill/billRecord'
import {addBillRecord, getBillRecord} from "../../api/bill/billRecord";

export default {
  name: "BillRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      //一下三个元素是关于多选框选择的
      ids: [],// 选中数组
      single: true,// 非单个禁用
      multiple: true,// 非多个禁用
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆进出场就记录表格数据
      billRecordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        perPage: 10,
        orderBy: 'create_time desc',//排序按照创建时间按倒叙
        receiveName: null,
        isAttend: null,
        billType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      bigImage: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {

    /** 查询车辆进出场就记录列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      listBillRecord(this.queryParams).then(response => {
        this.billRecordList = response.data.data.dataList
        this.total = response.data.data.count
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pageNum: 1,
        perPage: 10,
        receiveName: null,
        billType: null,
        money: null,
        businessTime: null,
        createTime: null,
        isAttend: null,
        memo: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账单";
    },
    /** 修改按钮 */
    handleUpdate(row) {
      this.reset();
      const billId = row.id || this.ids
      getBillRecord(billId).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
            addBillRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });

        }
      });
    },
    /** 删除按钮 */
    handleDelete(row) {
      const billIds = row.id || this.ids
      this.$confirm('是否确认删除账单编号为"' + billIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteBillRecord(billIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    }

  }
}
</script>

<style scoped>

</style>
